<?php

set_include_path(get_include_path() . PATH_SEPARATOR . '..');

require_once 'simpletest/autorun.php';
require_once 'database_engine/datareader_stub.php';

class DataReaderTests extends UnitTestCase
{
    public function testSimpleDataReader()
    {
        $dataReader = new EditableDataReaderStub(array(
            array("id" => 1, "value" => "string value 1"),
            array("id" => 2, "value" => "string value 2")
        ));
        
        $dataReader->Open();

        $this->assertEqual(true, $dataReader->Next());
        $this->assertEqual(1, $dataReader->GetFieldValueByName("id"));
        $this->assertEqual("string value 1", $dataReader->GetFieldValueByName("value"));
                
        $this->assertEqual(true, $dataReader->Next());
        $this->assertEqual(2, $dataReader->GetFieldValueByName("id"));
        $this->assertEqual("string value 2", $dataReader->GetFieldValueByName("value"));

        $this->assertEqual(false, $dataReader->Next());

        $dataReader->Close();
    }

    public function testDataReaderPrimaryKeys()
    {
        $dataReader = new EditableDataReaderStub(array(
            array("id" => 1, "value" => "string value 1"),
            array("id" => 2, "value" => "string value 2")
        ),
        array("id"));

        $dataReader->Open();

        $this->assertTrue($dataReader->IsPrimaryKeyField("id"));
        $this->assertFalse($dataReader->IsPrimaryKeyField("value"));

        $dataReader->Close();
    }

    public function testDataReaderFieldsOnNotOpenedDataset()
    {
        $dataReader = new EditableDataReaderStub(array(
            array("id" => 1, "value" => "string value 1"),
            array("id" => 2, "value" => "string value 2")
        ),
        array("id"));

        $this->assertIdentical(array("id", "value"), $dataReader->GetFieldNames());
    }
    
    public function testDataReaderFields()
    {
        $dataReader = new EditableDataReaderStub(array(
            array("id" => 1, "value" => "string value 1"),
            array("id" => 2, "value" => "string value 2")
        ),
        array("id"));

        $dataReader->Open();

        $fieldNames = $dataReader->GetFieldNames();
        $this->assertTrue(in_array("id", $fieldNames));
        $this->assertTrue(in_array("value", $fieldNames));

        $dataReader->Next();

        $dataReader->Close();
    }

    public function testDataReaderCondition()
    {
        $dataReader = new EditableDataReaderStub(array(
            array("id" => 1, "value" => "string value 1"),
            array("id" => 2, "value" => "string value 2")
        ),
        array("id"));

        $dataReader->Open();
        $dataReader->AddSimpleCondition('id', ConditionOperator::$Equals, 2);

        $this->assertTrue($dataReader->Next());

        $this->assertEqual(2, $dataReader->GetFieldValueByName('id'));
        $this->assertEqual('string value 2', $dataReader->GetFieldValueByName('value'));
        
        $this->assertFalse($dataReader->Next());
        
        $dataReader->Close();
    }

    public function testDataReaderInsert()
    {
        $dataReader = new EditableDataReaderStub(array(
            array("id" => 1, "value" => "string value 1"),
            array("id" => 2, "value" => "string value 2")
        ),
        array("id"));

        $dataReader->Open();

        $dataReader->Insert();

        $dataReader->SetFieldValueByName('id', 3);
        $dataReader->SetFieldValueByName('value', 'string value 3');
        
        $this->assertEqual(
            3,
            $dataReader->GetFieldValueByName('id')
        );
        $this->assertEqual(
            'string value 3',
            $dataReader->GetFieldValueByName('value')
        );

        $dataReader->Post();

        $this->assertEqual(
            3,
            $dataReader->GetFieldValueByName('id')
        );
        $this->assertEqual(
            'string value 3',
            $dataReader->GetFieldValueByName('value')
        );

        $dataReader->Next();
        $dataReader->Next();

        $this->assertEqual(
            2,
            $dataReader->GetFieldValueByName('id')
        );
        $this->assertEqual(
            'string value 2',
            $dataReader->GetFieldValueByName('value')
        );

        $dataReader->Close();
    }


    public function testDataReaderEdit()
    {
        $dataReader = new EditableDataReaderStub(array(
            array("id" => 1, "value" => "string value 1"),
            array("id" => 2, "value" => "string value 2")
        ),
        array("id"));

        $dataReader->Open();

        $dataReader->Next();

        $dataReader->Edit();

        $dataReader->SetFieldValueByName('id', 3);
        $dataReader->SetFieldValueByName('value', 'string value 3');

        $this->assertEqual(
            3,
            $dataReader->GetFieldValueByName('id')
        );
        $this->assertEqual(
            'string value 3',
            $dataReader->GetFieldValueByName('value')
        );

        $dataReader->Post();

        $this->assertEqual(
            3,
            $dataReader->GetFieldValueByName('id')
        );
        $this->assertEqual(
            'string value 3',
            $dataReader->GetFieldValueByName('value')
        );
        $dataReader->Next();

        $this->assertEqual(
            2,
            $dataReader->GetFieldValueByName('id')
        );
        $this->assertEqual(
            'string value 2',
            $dataReader->GetFieldValueByName('value')
        );

        $dataReader->Close();
    }
}

?>